enchant();

window.onload = function() {
	var game = new Game(320, 320);
	game.preload('../images/chara1.gif', '../images/icon0.gif', 'effect0.gif');
	game.fps = 24;
	game.onload = function() {

		var bear = new Sprite(32, 32);
		bear.image = game.assets['../images/chara1.gif'];
		bear.baseFrame = 0;
		bear.frame = bear.baseFrame;
		bear.addEventListener('enterframe', function(e) {

			// 移動系
			if (game.input.left) {
				this.x -= 5;
				// 少し工夫できる？
				this.frame = (this.frame + 1) % 3;
				this.scaleX = -1;
			}
			if (game.input.right) {
				this.x += 5;
				this.frame = (this.frame + 1) % 3;
				this.scaleX = 1;
			}

			// 未入力ってどう判断する？

			// TODO upでなくてクリックとかA,Bボタンとか
			if (game.input.up) {
				
				if(game.frame % 3 != 0){
					//TODO delay少し直さないと
					return;
				}
				
				// 弾はクマ中心
				var bullet = new Sprite(16, 16);
				bullet.image = game.assets['../images/icon0.gif'];
				bullet.frame = 54;
				bullet.scaleX = bear.scaleX;
				// 弾の初期位置(x)もう少しどうにか。
				if (bullet.scaleX == 1) {
					bullet.x = bear.x + 20;
				} else {
					bullet.x = bear.x - 7;
				}
				bullet.y = bear.y + 10;
				bullet.addEventListener('enterframe', function() {
					this.x += 10 * this.scaleX;
					// 画面外に飛んだら消さないと。
					if (this.x > game.width || this.x < 0) {
						game.rootScene.removeChild(this);
					}
					
					if (enemy && this.intersect(enemy)) {
						console.log('enemy intersect bullet');
						
						game.rootScene.removeChild(this);
						enemy.damage();
					}
				});
				game.rootScene.addChild(bullet);
			}

		});

		// 敵
		var enemy = new Sprite(32, 32);
		enemy.image = game.assets['../images/chara1.gif'];
		enemy.baseFrame = 5;
		enemy.frame = enemy.baseFrame;
		enemy.damageFrame = 0;
		enemy.hp = 3;

		enemy.damage = function() {
			console.log('enemy.damage');
			enemy.damaged = true;
			this.hp -= 1;

			if (this.hp == 0) {
				// TODO 死にアクション
				this.dead = true;
				
				var explode = new Sprite(16, 16);
				explode.scale(3,3);
				explode.image = game.assets['effect0.gif'];
				explode.x = this.x + (this.width / 2) - (explode.width / 2);
				explode.y = this.y + (this.height / 2) - (explode.height / 2);
				explode.addEventListener('enterframe', function() {

					if (this.age % 2 == 0) {
						this.frame += 1;
					}

					if (this.frame > 4) {
						this.parentNode.removeChild(this);
					}
				});

				this.parentNode.addChild(explode);
				this.parentNode.removeChild(this);
				console.log('enemy delete');
				//TODO 配列にしたらdelete効く
				enemy = undefined;
				//delete enemy;
			}
		}

		enemy.addEventListener('enterframe', function() {

			if (this.damaged) {
				this.damageFrame +=1;
				
				if(this.damageFrame == 5){
					// 喰らいモーションから復帰する
					this.frame = this.baseFrame;
					this.visible = true;
					this.damaged =false;
					this.damageFrame = 0;
					return;
				}
				
				this.frame = 8;
				if (this.age % 2 == 0) {
					this.visible = false;
				} else {
					this.visible = true;
				}
			}
		});

		var stage = new Group();
		stage.addChild(bear);
		stage.addChild(enemy);

		game.rootScene.addChild(stage);

		var pad = new Pad();
		pad.x = 0;
		pad.y = 224;
		game.rootScene.addChild(pad);
	}

	game.start();
};